package linter.linterjava.controller.user;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import linter.linterjava.POJO.DO.UserCard;
import linter.linterjava.POJO.DTO.UserCardDTO;
import linter.linterjava.POJO.DTO.UserDTO;
import linter.linterjava.custom.ResultVO;
import linter.linterjava.service.UserCardService;
import linter.linterjava.service.UserQueryService;
import linter.linterjava.service.UserService;
import linter.linterjava.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

import static linter.linterjava.custom.CustomResultType.USER_NOT_EXIST;

/**
 * @author 北煜
 */
@Slf4j
@RequestMapping("userCard")
@RestController
@Api(tags = "用户名片模块")
public class UserCardController {

    @Resource
    UserService userService;
    @Resource
    UserCardService userCardService;
    private final UserQueryService userQueryService;
    @Autowired
    public UserCardController(UserQueryService userQueryService) { this.userQueryService = userQueryService; }
    @GetMapping("/show/getUserByUsername/{username}")
    @ApiOperation("根据用户名查找用户")
    public ResultVO<UserDTO> getUserByUsername(@PathVariable String username) { return userQueryService.getUserByUsername(username); }

    @PostMapping("/getUserCard")
    @ApiOperation("获取用户名片")
    public ResultVO<UserCard> getUserCard(){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(userCardService.getUserCardById(uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/updateUserCard")
    @ApiOperation("更新用户名片")
    public ResultVO<String> updateUserCard(@RequestBody UserCardDTO userCardDTO){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(userCardService.updateUserCardById(userCardDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    // TODO：日历返回需要一个星期（7天）的数据

}
